<template>
	<view class="page">
		<view class="page-head">
			<u-navbar title="积分签到" leftIconSize="16" leftIconColor="#000000" :fixed="true" :placeholder="true"
				:bgColor="headBackground" :autoBack="true" :titleStyle="{color:'#000000'}">
			</u-navbar>
		</view>
		<view class="page-top-bg">
			<image class="head-bg" src="@/static/points/jifen-bg.jpg" mode=""></image>
		</view>

		<view class="page-ctx">
			<view class="ctx-inner">
				<view class="user-info">
					<view class="user-box" @click="toUser()">
						<view class="avatar-box">
							<image class="avatar" :src="mix_user_avatar" mode="aspectFill"></image>
						</view>
						<view class="info-box">
							<view class="title-box">
								<view class="days">
									已累计签到<text class="number">{{rili_info.days}}</text>天
								</view>
								<view class="desc">
									明日签到可获得{{rili_info.next_day}}积分
								</view>
							</view>
						</view>
						<view class="value-box">
							<image class="avatar" src="@/static/points/points.png" mode=""></image>
							<text>362</text>
						</view>
					</view>
				</view>

				<view class="box-item day-box">
					<view class="main-title flex-between">
						<view class="title-text">
							连续签到领好礼
						</view>
						<view class="tixing-box flex">
							<view class="tixing-text">
								签到提醒
							</view>
							<view class="switch-box">
								<u-switch v-model="is_open_tixing" :activeValue="1" :inactiveValue="0" size="16" activeColor="#164782"
									@change="changeSwitch"></u-switch>
							</view>
						</view>
					</view>

					<view class="sign-list">
						<view class="sign-item" v-for="(item,index) in current_week_date_list"
							:class="[{'sign-item-last': index == 6},{'active': item.is_qiandao != 0}, item.status]"
							@click="do_sign(item)">
							<view v-if="index != 6" class="sign-info column-flex-center">
								<view class="title">
									第{{index + 1}}天
								</view>
								<view class="icon-box">
									<image src="@/static/points/points.png" mode=""></image>
								</view>
								<view class="status">
									{{item.is_qiandao == 1 ? '已签到' : item.jifen + '积分'}}
								</view>
							</view>
							<view v-else class="sign-info sign-info-last flex-center">
								<view class="sign-info-left column-flex-center">
									<view class="title">
										第{{index + 1}}天
									</view>
									<view class="icon-box">
										多签多得
									</view>
									<view class="status">
										{{item.is_qiandao == 1 ? '已签到' : item.jifen + '积分'}}
									</view>
								</view>
								<view class="sign-info-right">
									<image src="@/static/points/big-points.png" mode=""></image>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="box-item task-box">
					<view class="main-title">
						做任务赚积分
					</view>
					<view class="task-list">
						<view class="task-item">
							<view class="icon-box">
								<image src="@/static/points/task-invite.png" mode=""></image>
							</view>
							<view class="info-box">
								<view class="title">
									邀请好友
								</view>
								<view class="desc">
									邀请好友开通会员获得对应积分
								</view>
							</view>
							<view class="btn flex-center" @click="toRoute('/invitation-qrcode')">
								去邀请
							</view>
						</view>
						<view class="task-item">
							<view class="icon-box">
								<image src="@/static/points/task-sign.png" mode=""></image>
							</view>
							<view class="info-box">
								<view class="title">
									累积签到
								</view>
								<view class="desc">
									连续签到7天额外获得10积分
								</view>
							</view>

						</view>

						<view class="task-item">
							<view class="icon-box">
								<image src="@/static/points/task-buy.png" mode=""></image>
							</view>
							<view class="info-box">
								<view class="title">
									购买商品
								</view>
								<view class="desc">
									购买商品获得对应积分
								</view>
							</view>
							<view class="btn flex-center" @click="toRoute('/index')">
								去购物
							</view>
						</view>
					</view>
				</view>

				<view class="box-item rule-box">
					<view class="main-title">
						积分规则
					</view>
					<view class="html-box">
						<u-parse :content="points_rule.content" :tagStyle="richStyle"></u-parse>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import dayjs from 'dayjs';

	export default {
		components: {

		},
		data() {
			return {
				defaultHeadBackground: 'transparent',
				headBackground: 'rgba(255, 255, 255, 0)', //控制顶部标题栏颜色 默认透明颜色 transparent
				//
				info: {},
				is_open_tixing: 0,
				rili_info: {}, //签到日历信息
				points_rule: {},
				all_date_list: [],
				current_week_date_list: [],

				//
				richStyle: {
					// 字符串的形式
					p: 'color: #000000;font-size: 12px;line-height:1.6;font-family: PingFangSC-Regular;text-align:justify;',
					span: 'color: #000000;font-size: 12px;line-height:1.6;font-family: PingFangSC-Regular;',
					img: 'vertical-align: bottom;'
				},
			}
		},

		computed: {

		},
		onLoad() {
			this.query_points_content();
			this.query_qiandao_rili()
		},
		onShow() {
			this.query_user()
		},
		onPageScroll(e) {
			console.log(e)
			let percent = e.scrollTop / 40;
			percent = percent < 1 ? percent : 1

			this.headBackground = `rgba(255, 255, 255,${percent})`

			if (e.scrollTop != 0) {

			} else {
				// this.headBackground = ''
			}
		},
		methods: {
			query_user() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'users_userInfo',
					},
				}).then(res => {
					if (res.code == 200) {
						this.my_info = res.data;
					}
				})
			},

			query_points_content() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'news_detail',
						id: 96, //this.id,
					},
				}).then(res => {
					if (res.code == 200) {
						this.points_rule = res.data.info;
					}
				})
			},
			//积分签到日历
			query_qiandao_rili() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'jiFen_qiandaoWeek',
					},
				}).then(res => {
					if (res.code == 200) {
						this.rili_info = res.data;
						this.all_date_list = res.data.list

						this.get_current_week_days()
					}
				})
			},

			get_current_week_days() {
				let current_date = dayjs().format('YYYY-MM-DD')
				let first_date_in_week = dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD'); //本周周一对应日期
				let last_date_in_week = dayjs(first_date_in_week).add(7, 'day').format('YYYY-MM-DD'); //本周周日对应日期

				// console.log('本周第一天', first_date_in_week)
				// console.log('本周最后一天', last_date_in_week)

				let first_date_index = this.all_date_list.findIndex(v => v.day == first_date_in_week)
				this.current_week_date_list = this.all_date_list.slice(first_date_index, first_date_index + 7);

				this.current_week_date_list.forEach(v => {
					if (v.day < current_date) {
						v.status = 'prev'
					} else if (v.day == current_date) {
						v.status = 'curr'
					} else {
						v.status = 'next'
					}
				})
			},


			do_sign(item) {
				if (item.status != 'curr') {
					return
				}
				if (item.status == 'curr' && item.is_qiandao != 0) {
					this.$alert('今日已签到')
					return
				}

				this.mix_throttle(this.do_sign_fn);
			},

			do_sign_fn() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'jiFen_qiandao',
					},
				}).then(res => {
					this.$alert(res)
					if (res.code == 200) {
						this.query_qiandao_rili()
					}
				})
			},

			changeSwitch(e) {
				// console.log(e)
				// this.form.moren = e ? 1 : 0
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F8F8F8;
	}
</style>

<style lang="scss" scoped>
	// 设置标题左对齐
	.page-head ::v-deep .u-navbar__content__left {
		// display: none;
	}

	.page-head ::v-deep .u-navbar__content__title {
		// flex: 1;
		// padding-left: 32rpx;
		// text-align: left;
		// font-family: PingFang SC, PingFang SC;
		// font-weight: 400;
		// font-size: 32rpx;
		// color: #000000;
	}

	// 设置标题左对齐



	.page-top-bg {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 422rpx;

		// background: linear-gradient(180deg, #004281 0%, rgba(110, 204, 142, 0) 100%);

		.head-bg {
			width: 100%;
			height: 100%;
		}
	}

	.page-top {
		position: fixed;
		z-index: 200;
		top: 0;
		left: 0;
		right: 0;
		height: 172rpx;

		&.bg {
			background: #00AFEB;
		}


		.page-header {
			padding: 0 32rpx;
			padding-top: 100rpx;


			.menu-area-box {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				justify-content: center;
			}

			.addr-box {
				display: flex;
				align-items: center;
				justify-content: flex-start;


				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 34rpx;
				color: #FFFFFF;

				// .page-title {
				// 	font-size: 34rpx;
				// 	font-family: PingFang SC-Bold, PingFang SC;
				// 	font-weight: bold;
				// 	color: #000000
				// }

				.header-img {
					width: 213rpx;
					height: 48rpx;
				}
			}

			.head-search {
				margin-top: 28rpx;

				::v-deep .uicon-search {
					font-size: 40rpx !important;
				}
			}

		}

	}


	.page {
		.page-ctx {
			position: relative;
			padding: 0 32rpx;
			// padding-top: 160rpx;
			padding-top: 40rpx;

			.ctx-inner {
				// background: #F5F5F5;
			}
		}
	}


	.user-info {

		.user-box {
			padding: 0rpx 0rpx;
			display: flex;
			align-items: center;

			.avatar-box {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				overflow: hidden;

				.avatar {
					width: 100rpx;
					height: 100rpx;
				}
			}


			.info-box {
				flex: 1;
				overflow: hidden;
				padding-left: 30rpx;

				.title-box {
					.days {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 28rpx;
						color: #000000;

						.number {
							color: #164782;
						}
					}

					.desc {
						margin-top: 10rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 20rpx;
						color: #000000;


					}
				}


			}

			.value-box {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 176rpx;
				height: 64rpx;
				// background: rgba(255, 255, 255, 0.24);
				background: transparent;
				border-radius: 32rpx 32rpx 32rpx 32rpx;
				border: 1rpx solid #164782;


				image {
					margin-right: 12rpx;
					width: 35rpx;
					height: 36rpx;
				}

				text {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 24rpx;
					color: #000000;
				}
			}

		}

	}




	.box-item {
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 32rpx 24rpx;
	}

	.day-box {
		margin-top: 24rpx;



		.main-title {
			margin-bottom: 24rpx;

			.title-text {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #164782;
			}

			.tixing-box {
				.tixing-text {
					margin-right: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
				}

				.switch-box {}
			}
		}

		.sign-list {
			display: flex;
			flex-wrap: wrap;

			.sign-item {
				margin-right: 18rpx;
				margin-bottom: 18rpx;

				&:nth-child(4n) {
					margin-right: 0;
				}

				&:nth-child(-n + 4) {
					margin-top: 0;
				}

				.sign-info {
					width: 146rpx;
					height: 182rpx;
					background: #F8F8F8;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					border: 1rpx solid #F8F8F8;
				}


				&.prev {
					.title {
						color: #9695A3;
					}

					.icon-box {
						filter: grayscale(1);
					}

					.status {
						color: #9695A3;
					}
				}

				&.curr {
					.sign-info {
						border: 1rpx solid #164782;
					}

					&.active {
						.sign-info {
							background: #164782;
						}

						.title {
							color: #FFFFFF;
						}

						.icon-box {
							color: #FFFFFF !important;
						}

						.status {
							color: #FFFFFF;
						}
					}
				}




				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #000000;
				}

				.icon-box {
					text-align: center;
					margin: 20rpx 0;
					width: 100%;
					height: 44rpx;

					image {
						width: 43rpx;
						height: 44rpx;
					}
				}

				.status {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 20rpx;
					color: #333333;
				}

			}

			.sign-item-last {
				margin-right: 0;
				width: 310rpx;
				height: 182rpx;
				background: #F8F8F8;
				border-radius: 8rpx 8rpx 8rpx 8rpx;


				.sign-info-last {
					width: 310rpx;
					height: 182rpx;
				}


				.sign-info-left {
					.icon-box {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 20rpx;
						color: #333333;
					}
				}

				.sign-info-right {
					margin-left: 20rpx;

					image {
						width: 132rpx;
						height: 110rpx;
					}
				}

			}
		}
	}



	.task-box {
		margin-top: 24rpx;
		padding-bottom: 0;


		.main-title {
			margin-bottom: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
		}


		.task-list {
			.task-item {
				padding: 32rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;


				&+.task-item {
					border-top: 1rpx solid #F5F5F5;
				}


				.icon-box {
					image {
						width: 80rpx;
						height: 80rpx;
					}
				}

				.info-box {
					padding-left: 32rpx;
					flex: 1;
					overflow: hidden;
					padding-right: 20rpx;

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 28rpx;
						color: #000000;
					}

					.desc {
						margin-top: 6rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #AEAEAE;
					}
				}

				.btn {
					width: 146rpx;
					height: 56rpx;
					background: #164782;
					border-radius: 52rpx 52rpx 52rpx 52rpx;

					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #FFFFFF;
				}
			}
		}
	}

	.rule-box {
		margin-top: 24rpx;

		.main-title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
		}
	}
</style>